package com.inossem.warehousestorage_inossem.adapter.inventory;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryExtra;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryItemAddActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySpeInvFlagListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class InventorySelectSpecialInvFlagAdapter extends BaseAdapter {

    private Activity mContext;
    private List<InventorySpeInvFlagListBean.DataBean.SpecialInventoryFlagListBean> list = new ArrayList<InventorySpeInvFlagListBean.DataBean.SpecialInventoryFlagListBean>();
    private LayoutInflater inflater;

    public InventorySelectSpecialInvFlagAdapter(Activity context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            InventorySpeInvFlagListBean.DataBean.SpecialInventoryFlagListBean materielItem = list.get(position);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_inventory_select_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InventorySpeInvFlagListBean.DataBean.SpecialInventoryFlagListBean item) {
        viewHolder.code.setText(item.getSpecialInventoryFlag());
        viewHolder.description.setText(item.getSpecialInventoryDescription());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(InventoryExtra.CODE, item.getSpecialInventoryFlag());
                intent.putExtra(InventoryExtra.DESCRIPTION, item.getSpecialInventoryDescription());
                mContext.setResult(InventoryItemAddActivity.SELECT_SPECIAL_INVENTORY_FLAG, intent);
                mContext.finish();
            }
        });
    }

    private class ViewHolder {
        private TextView code, description;
    }

    public void setList(List<InventorySpeInvFlagListBean.DataBean.SpecialInventoryFlagListBean> list) {
        this.list = list;
    }
}